package com.learn.databinding.workwithobservabledataobjects.bindlayoutwithstateflow

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

class BindLayoutWithStateFlowViewModel : ViewModel() {
    /**
     * 使用stateFlow包装数据
     */
    private val _userName = MutableStateFlow<String>("")
    val userName: StateFlow<String> = _userName
    init {
        //设置userName初始值
        viewModelScope.launch {
            _userName.value = "花木兰"
        }
    }

    /**
     * 2s后更新userName
     */
    fun updateUserName(){
        viewModelScope.launch {
            delay(2000)
            _userName.value = "加里奥"
        }
    }
}